<template>
  <div class="center-box">
    <ToolBar>
      <div slot="unselect" class="unselect">
        <div class="title">关注的联系人</div>
      </div>
    </ToolBar>
    <div class="list">
      <div class="item" v-for="(item,index) in customerList">
        <div class="avatar" v-text="item.name.substr(0,1)">
        </div>
        <div class="info">
          <div class="name">
            {{item.name}}
            <i class="el-icon-star-on" style="color: orange;font-size: 18px"></i>
          </div>
          <div class="contact" v-if="item.email || item.mobile">
            <div class="email" v-text="item.email"></div>
            <div class="gap" v-if="item.mobile"></div>
            <div class="mobile" v-text="item.mobile"></div>
          </div>
        </div>
        <div class="option-group">
          <div class="star option">
            <i class="el-icon-star-on" style="color: orange;font-size: 20px" @click="unStarCustomer(item.customerId, index)"></i>
          </div>
          <div class="view option"  @click="$router.push({'path':'/ticket-view?ticketId=' + ticket.ticketId})">
            <i class="el-icon-view"></i>
          </div>
          <div class="edit option" @click="showEditCustomer(item)">
            <i class="el-icon-edit"></i>
          </div>
          <div class="delete option" @click="deleteCustomer(index)">
            <i class="el-icon-delete"></i>
          </div>
        </div>
      </div>
      <div v-if="customerList.length == 0" style="text-align: center;line-height: 60px;font-size:14px;color:#666666">
        <i v-if="loading" style="font-size: 26px;margin-top: 30px" class="el-icon-loading"></i>
        <span v-else>暂无查询结果</span>
      </div>
    </div>
    <el-dialog :modal-append-to-body="false" :title="(customerForm && customerForm.customerId ? '修改':'添加') +'联系人'" @close="customerForm = null" :visible="customerForm!=null">
      <CustomerEdit @saveCustomer="saveCustomer" v-if="customerForm"  :customer-form="customerForm"></CustomerEdit>
    </el-dialog>
  </div>
</template>

<script>
import {findCustomerList, deleteCustomer, starCustomer, unStarCustomer, findCustomerFollowList} from "@/api/customer";
import ToolBar from '@/components/common/tool-bar'
import CustomerEdit from './edit'

export default {
  name: "follow",
  components: {
    ToolBar,
    CustomerEdit
  },
  data() {
    return {
      customerList: [],
      loading: false,
      customerForm: null
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    loadData() {
      this.loading = true
      findCustomerFollowList().then(res => {
        this.customerList = res.data
      }).finally(()=>{
        this.loading = false
      })
    },
    unStarCustomer(customerId, index) {
      unStarCustomer(customerId).then(res => {
        if(res.code == 200) {
          this.$message.success('取消关注成功')
          this.customerList.splice(index,1)
        }
      })
    },
    deleteCustomer(index) {
      this.$confirm('是否删除该联系人?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCustomer(this.customerList[index].customerId).then(res => {
          if(res.code == 200) {
            this.$message.success('删除联系人成功')
            this.customerList.splice(index,1)
          }
        })
      })
    },
    showEditCustomer(form) {
      this.customerForm = form ? form : {}
    },
    saveCustomer() {
      this.loadData()
      this.customerForm = null
    }
  }
}
</script>

<style scoped lang="less">

.unselect {
  display: flex;
  align-items: center;
  .title {
    flex: 1;
    color: #606266;
    font-size: 14px;
  }
  .btn-group {
    display: flex;
    align-items: center;
  }
}
.list {
  width: 100%;
  box-sizing: border-box;

  @media(max-width: 679px) {
  }
  .item {
    display: flex;
    padding: 15px 20px;
    align-items: center;
    border-bottom: 1px solid #F0F0F0;
    &:hover {
      background: #F8F8F8;
    }
    .avatar {
      width: 50px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      border: 1px solid #F0F0F0;
      border-radius: 50%;
    }
    .info {
      padding-left: 10px;
      flex: 1;
      .name {
        margin-bottom: 6px;
        font-size: 16px;
      }
      .contact {
        display: flex;
        font-size: 12px;
        color: #666666;
        align-items: center;
        .gap {
          width: 5px;
          height: 5px;
          margin: 0 5px;
          background: #888888;
          border-radius: 50%;
        }
      }
    }
    &:hover {
      .option-group {
        opacity: 1;
      }
    }
    .option-group {
      flex-shrink: 0;
      display: flex;
      opacity: 0;
      transition: opacity .2s;
      .option {
        margin-right: 14px;
        cursor: pointer;
      }
      .edit:hover {
        color: #0091ff;
      }
      .delete:hover {
        color: red;
      }

      @media(max-width: 679px) {
        padding: 0 5px;
      }
    }
  }
}
</style>
